﻿using SmartSapce.ServiceLocation;
using SmartSpace.Core;
using SmartSpace.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SmartSpace.WebApp.Modules.Core.Pages
{
    public partial class CodeDefinition_Create : SsTemplatePage
    {
        public Guid CategoryId { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            CategoryId = RequestUtility.GetGuid(Request, "categoryid");

            if (!IsPostBack)
            {
                ICoreService service = ServiceLocator.Get<ICoreService>();
                CodeCategory category = service.GetCodeCategory(CategoryId);
                _ctlEditCategory.Text = category.Caption;
            }
        }

        protected void _ctlBtnSave_Click(object sender, EventArgs e)
        {
            ICoreService service = ServiceLocator.Get<ICoreService>();
            CodeDefinition code = new CodeDefinition();
            code.Id = Guid.NewGuid();
            code.Code = _ctlEditCode.Text;
            code.Caption = _ctlEditCaption.Text;
            code.CategoryId = CategoryId;
            if (service.SaveCodeDefinition(code).IsSuccess)
            {
                Response.Redirect(string.Format("CodeDefinition-List.aspx?category={0}", CategoryId));
            }
        }
    }
}